package com.itheima.case2.web;

import com.itheima.case2.pojo.po.Permission;
import com.itheima.case2.pojo.vo.*;
import com.itheima.case2.service.PermissionService;
import com.itheima.case2.utils.BaseController;
import com.itheima.case2.utils.BeansFactory;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/permission/*")
public class PermissionServlet extends BaseServlet{
    //查询所有权限信息
    public void queryAllPermission(HttpServletRequest request, HttpServletResponse response){
        try {
            //使用工厂类获取对象
            PermissionService permissionService = BeansFactory.getInstance("permissionService");
            //调用方法查询
            List<Permission> list = permissionService.queryAllPermission();
            //将查询到的数据返回给前端
            Result result = new Result(true, "权限信息查询成功", list);
            BaseController.printResult(response,result);
        } catch (Exception e) {
            e.printStackTrace();
            Result result = new Result(false, "权限信息查询失败");
            try {
                BaseController.printResult(response,result);
            } catch (Exception ioException) {
                ioException.printStackTrace();
            }
        }
    }

    //获取角色分页数据
    public void queryPerAndRoleByPage(HttpServletRequest request, HttpServletResponse response){
        try {
            //从前端获取数据
            QueryPageBean pageBean = BaseController.parseJSON2Object(request, QueryPageBean.class);
            //使用工厂类获取对象
            PermissionService permissionService = BeansFactory.getInstance("permissionService");
            //调用方法查询
            PageResult pageResult = permissionService.queryPerAndRoleByPage(pageBean);
            //将查询到的数据返回给前端
            Result result = new Result(true, "权限信息分页查询成功！", pageResult);
            BaseController.printResult(response, result);
        } catch (Exception e) {
            e.printStackTrace();
            Result result = new Result(false, "权限信息分页查询失败！");
            try {
                BaseController.printResult(response, result);
            } catch (Exception ioException) {
                ioException.printStackTrace();
            }
        }
    }

    //修改权限和对应角色信息
    public void updatePerAndRole(HttpServletRequest request, HttpServletResponse response) {
        try {
            //使用工具类获取前端传入的数据
            UpdatePermission updatePermission = BaseController.parseJSON2Object(request, UpdatePermission.class);
            //使用工厂创建对象并获取
            PermissionService permissionService = BeansFactory.getInstance("permissionService");
            permissionService.updatePerAndRole(updatePermission);
            Result result = new Result(true, "权限信息修改成功！");
            BaseController.printResult(response, result);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                Result result = new Result(false, "权限信息修改失败！");
                BaseController.printResult(response, result);
            } catch (Exception ioException) {
                ioException.printStackTrace();
            }
        }
    }

    //添加权限和对应角色信息
    public void insertPerAndRole(HttpServletRequest request, HttpServletResponse response) {
        try {
            //使用工具类获取前端传入的数据
            AddPermission addPermission = BaseController.parseJSON2Object(request, AddPermission.class);
            //使用工厂创建对象并获取
            PermissionService permissionService = BeansFactory.getInstance("permissionService");
            permissionService.insertPerAndRole(addPermission);
            Result result = new Result(true, "恭喜你，权限添加成功！");
            BaseController.printResult(response, result);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                Result result = new Result(false, "权限添加失败！");
                BaseController.printResult(response, result);
            } catch (Exception ioException) {
                ioException.printStackTrace();
            }
        }
    }

    //删除权限和对应角色信息
    public void deleteById(HttpServletRequest request, HttpServletResponse response) {
        try {
            //获取前端参数
            String roleId = request.getParameter("perId");
            //使用工厂创建对象并获取
            PermissionService permissionService = BeansFactory.getInstance("permissionService");
            permissionService.deleteById(roleId);
            Result result = new Result(true, "恭喜你，权限删除成功！");
            BaseController.printResult(response,result);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                Result result = new Result(false, "权限删除失败！");
                BaseController.printResult(response,result);
            } catch (Exception ioException) {
                ioException.printStackTrace();
            }
        }
    }

}
